<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h2 class="title">熊猫助手</h2>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
          @keyup.enter="handleLogin">
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="large" type="primary" style="width:100%;color: black;" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 熊猫助手 pandarobot.chat And xiaoyide.cn All Rights Reserved.</span>
    </div>
  </div>
</template>
  
<script setup lang="ts">
import { ref } from "vue";
import Cookies from 'js-cookie';
import { FormRules } from 'element-plus';
import { FormInstance } from 'element-plus';
import { useAuthStore } from '@/store/modules/auth';
import { LoginData } from '@/api/types';
import { ElMessage } from 'element-plus';
import { to } from 'await-to-js';


const authStore = useAuthStore();
const loading = ref(false);
const loginRef = ref<FormInstance>();
const loginForm = ref({
  tenantId: '000000',
  clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  username: 'admin',
  password: '',
  rememberMe: false,
  code: '',
  uuid: '',
  grantType: 'password'
} as LoginData);
const loginRules: FormRules = {
  username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }]
};
const handleLogin = () => {
  loginRef.value?.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("tenantId", String(loginForm.value.tenantId), { expires: 30 });
        Cookies.set('username', String(loginForm.value.username), { expires: 30 });
        Cookies.set('password', String(loginForm.value.password), { expires: 30 });
        Cookies.set('rememberMe', String(loginForm.value.rememberMe), { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("tenantId");
        Cookies.remove('username');
        Cookies.remove('password');
        Cookies.remove('rememberMe');
      }
      // 调用action的登录方法
      const [err] = await to(authStore.login(loginForm.value));
      if (err ==null) {
        // 登录成功
        location.reload();
      } else {
        loading.value = false;
        ElMessage({
        message: err.toString(),
        type: 'error',
        })
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>
  
<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/background2.jpeg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-size: 22px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial, serif;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
  